package com.gduf.lsm.service;

import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.constant.JoinEventsStatusConstant;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.entity.JoinCultural;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.JoinCulturalMapper;
import com.gduf.lsm.pojo.dto.JoinStatusDTO;
import com.gduf.lsm.pojo.vo.JoinCulturalShowVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author lsm
 * @Data
 * 文化活动报名表
 */
@Service
public class JoinCulturalService {
    @Autowired
    private JoinCulturalMapper joinCulturalMapper;

    /**
     * 新增
     * @param joinCultural
     */
    public void add(JoinCultural joinCultural) {
        //先判断是否重复报名
        JoinCultural jc=joinCulturalMapper.selectByCulIdAndUserIdAndOldId(joinCultural.getCulId(),joinCultural.getUserId(),joinCultural.getOldId());
        if(ObjectUtil.isNotNull(jc)){
            throw new CustomException(ResultCodeEnum.JOIN_CULTURAL_OLDER_AGAIN);
        }
        //公益活动报名状态不可为空
        if(ObjectUtil.isEmpty(joinCultural.getStatus())){
            throw new CustomException(ResultCodeEnum.JOIN_ACTIVITY_STATUS_NULL_ERROR);
        }
        joinCulturalMapper.insert(joinCultural);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        joinCulturalMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            joinCulturalMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param joinCultural
     */
    public void updateById(JoinCultural joinCultural) {
        joinCulturalMapper.updateById(joinCultural);
    }

    /**
     * 修改活动状态
     * @param joinStatusDTO
     */
    public void updateStatus(JoinStatusDTO joinStatusDTO) {
        //未通过状态下备注不可为空
        if(joinStatusDTO.getStatus().equals(JoinEventsStatusConstant.JOIN_EVENTS_STATUS_NOT_PASSED)&&ObjectUtil.isEmpty(joinStatusDTO.getRemark())){
            throw new CustomException(ResultCodeEnum.JOIN_ACTIVITY_REMARK_NULL_ERROR);
        }
        //把前端实体拷贝
        JoinCultural joinCultural=new JoinCultural();
        BeanUtils.copyProperties(joinStatusDTO,joinCultural);
        joinCulturalMapper.updateById(joinCultural);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public JoinCultural selectById(Integer id) {
        return joinCulturalMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param joinCultural
     * @return
     */
    public List<JoinCultural> selectAll(JoinCultural joinCultural) {
        return joinCulturalMapper.selectAll(joinCultural);
    }

    /**
     * 分页查询
     * @param joinCultural
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<JoinCulturalShowVO> selectPage(JoinCultural joinCultural, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JoinCulturalShowVO> list=joinCulturalMapper.selectPageQuery(joinCultural);
        return PageInfo.of(list);
    }

    /**
     * 按用户ID查询参与过的活动，映射VO
     * @param userId
     * @return
     */
    public List<JoinCulturalShowVO> selectByUserId(Integer userId) {
        JoinCultural joinCultural=new JoinCultural();
        joinCultural.setUserId(userId);
        List<JoinCulturalShowVO> list=joinCulturalMapper.selectPageQuery(joinCultural);
        return list;
    }
}
